def update_ams_client():
    import urllib
    import zipfile
    import os.path
    import os
    import time

    url = 'http://115.182.69.8/download/attend_ctm_software/update/ams-client.zip'
    upfname = r"D:\Program Files (x86)\Attend\AttendManage\ams-client.zip"

    urllib.urlretrieve(url, upfname)

    try:
        cmd_killtask = r"taskkill /F /IM CardPoolView.exe"
        os.system(cmd_killtask)
    except Exception as e:
        print "kill_ams_client error:", str(e)

    time.sleep(0.3)
    
    zfile = zipfile.ZipFile(upfname, 'r')
    basedir = os.path.dirname(upfname)
    for p in zfile.namelist():
        if not p.endswith('/'):   
            f = os.path.join(basedir, p)   
            dir = os.path.dirname(f)   
            if not os.path.exists(dir):   
                os.makedirs(dir)   
            file(f, 'wb').write(zfile.read(p))

try:
    update_ams_client()
except Exception as e:
    print "update error:", str(e)
